const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const comments = require('../model/comments');
const Demo = require('../model/project_and_wz');
const {
    json
} = require('../util/entry');
const sendQQEmail = require('../util/sendEmail');
require('../model/link');

//查找评论
const selectComment = async function (params) {
    try {
        const data = await comments.findAll({
            where: {
                projectAndArticleId: params
            },
            order: [
                ['id', 'DESC']
            ], //倒序输出
        })
        return json(data);

    } catch (err) {
        return json(err);
    }
}

//添加评论
const addComment = async function (params) {
    try {
        const data = await comments.create({
            content: params.content,
            nick: params.nick,
            header: params.header,
            email: params.email,
            time: params.time,
            other: params.other,
            projectAndArticleId: params.projectAndArticleId,
        });
        return json(data);
    } catch (err) {
        return json(err);
    }

}

//浏览量增加功能
const updateLook = async function (id, isUp = true) {
    try {
        const data = await Demo.findAll({
            where: {
                id
            }
        })

        let number = 0;
        //判断添加还是删除
        if (isUp) {
            number = (+json(data)[0].message) + 1;
        } else {
            number = (+json(data)[0].message) - 1;
        }

        const ins = await Demo.update({
            message: number
        }, {
            where: {
                id
            }
        });
        return json(ins);
    } catch (err) {
        return json(err);
    }
}

//所有评论
const get_all_comment = async function () {
    try {
        const data = await comments.findAll({
            where: {
                // projectAndArticleId: params
            },
            order: [
                ['id', 'DESC']
            ], //倒序输出
            include: [Demo]
        })
        let arr = [];
        json(data).forEach((ele) => {
            arr.push({
                id: ele.id,
                content: ele.content,
                email: ele.email,
                title: (ele.project_and_article.title) != 'null' ? (ele.project_and_article.title) : '留言板内容',
                webmaster: ele.webmaster,
                nick: ele.nick
            })
        });
        return arr;

    } catch (err) {
        return json(err);
    }
}

//删除评论
const del_comment = async function (id) {
    const data = await comments.destroy({
        where: {
            id
        }
    });

}

//站长回复
const hf_comment = async function (id, email, txt, title) {
    try {
        const ins = await comments.update({
            webmaster: txt
        }, {
            where: {
                id
            }
        });
        // 发送邮件
        sendQQEmail(email, {
            message: txt,
            txt: title
        })
        return [];
    } catch (err) {
        return json(err);
    }
}

module.exports = {
    selectComment,
    addComment,
    updateLook,
    get_all_comment,
    del_comment,
    hf_comment
}